package com.wjd.wshow.common.resource.loader;

import com.wjd.wshow.common.resource.Resource;
import com.wjd.wshow.common.resource.pojo.UploadResource;

import java.io.IOException;
import java.io.OutputStream;

public class Uploader {

    /**
     * 上传
     * @param uploadResource 资源
     * @return 保存后的资源
     */
    public UploadResource upload(UploadResource uploadResource) throws IOException {
        try {
            ResourceLoader.getLoader().storeResource(uploadResource);
            return uploadResource;
        } catch (Exception e) {
            throw e;
        } finally {
            uploadResource.close();
        }
    }

    /**
     * 下载
     * @param resource 资源
     * @param outputStream 输出流
     * @return true/false
     */
    public boolean download(Resource resource, OutputStream outputStream) throws IOException {
        try {
            ResourceLoader.getLoader().loadResource(resource, outputStream);
            return true;
        } catch (Exception e) {
            throw e;
        }
    }

}
